<%--
  Created by IntelliJ IDEA.
  User: A
  Date: 2024/4/15
  Time: 20:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="../../static/layui/css/layui.css">
    <script src="../../static/jquery-2.1.4.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../static/layui/layui.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../static/mylayer.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../static/kindeditor/kindeditor.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>

    <table class="layui-hide" id="test" lay-filter="test"></table>
    <%--头部工具条--%>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container layui-inline">
            <button class="layui-btn" lay-event="add">添加</button>
            <button class="layui-btn layui-btn-danger" lay-event="deleteAll">批量删除</button>
        </div>
        <%--搜索form表单--%>
        <form class="layui-form layui-row layui-col-space16 layui-inline">
            <div class="layui-inline">博客名称</div>
            <div class="layui-inline" style="width: 200px;">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-form"></i>
                    </div>
                    <input type="text" name="title" value="" placeholder="请输入" class="layui-input" lay-affix="clear">
                </div>
            </div>
            <div class="layui-inline">博客内容</div>
            <div class="layui-inline" style="width: 200px;">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-username"></i>
                    </div>
                    <input type="text" name="content" placeholder="请输入" lay-affix="clear" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn" lay-submit lay-filter="submitSearch">Search</button>
                <button type="reset" class="layui-btn layui-btn-primary">Clear</button>
            </div>
        </form>
    </script>
    <%--右侧工具条--%>
    <script type="text/html" id="barDemo">
        <div class="layui-clear-space">
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="details">详情</a>
        </div>
    </script>
    <%--添加form表单--%>
    <div id="addForm" style="display: none">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <br>
                <label class="layui-form-label">博客名称</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" name="title" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">封面</label>
                <div class="layui-input-inline layui-input-wrap">
                    <div style="width: 132px;">
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" width="150px" height="150px" class="layui-upload-img" id="demo1">
                            <div id="demoText"></div>
                            <input type="hidden" name="image" id="imageId">
                        </div>
                    </div>
                    <button type="button" class="layui-btn" id="uploadId">
                        <i class="layui-icon layui-icon-upload"></i> 封面上传
                    </button>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">博客内容</label>
                <div class="layui-input-inline layui-input-wrap">
                    <textarea id="contentId" name="content" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit lay-filter="submitAdd">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
    <%--修改form表单--%>
    <div id="updateForm" style="display: none">
        <form class="layui-form" lay-filter="updateFormFilter" action="">
            <div class="layui-form-item">
                <br>
                <input type="hidden" name="id">
                <label class="layui-form-label">博客名称</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" name="title" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">博客内容</label>
                <div class="layui-input-inline layui-input-wrap">
<%--                    <input type="text" name="content" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">--%>
                    <textarea name="content"></textarea>
                </div>
            </div>
            <div class="layui-inline" style="width: 60px;">博客分类</div>
            <div class="layui-inline" style="width: 130px;">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-form"></i>
                    </div>
                    <select id="typeId" name="typeId" lay-filter="typeId">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline" style="width: 60px;">创建时间</div>
            <div class="layui-inline" style="width: 130px;">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-date"></i>
                    </div>
                    <input type="text" name="C" readonly placeholder="Field C" class="layui-input demo-table-search-date">
                </div>
            </div>
            <div class="layui-inline" style="width: 60px;">更新时间</div>
            <div class="layui-inline" style="width: 130px;">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-date"></i>
                    </div>
                    <input type="text" name="C" readonly placeholder="Field C" class="layui-input demo-table-search-date">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit lay-filter="submitUpdate">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
    <script type="text/html" id="imageTemplet">
        <img src="/pic/{{d.image}}">
    </script>
    <script type="text/html" id="statusTemplet">
<%--        {{#     if (d.status == 1) {                }}--%>
<%--            <input type="checkbox" name="BBB" title="上架|下架" lay-skin="switch" checked disabled>--%>
<%--        {{#     } else if (d.status == 0) {         }}--%>
<%--            <input type="checkbox" name="CCC" title="上架|下架" lay-skin="switch" disabled>--%>
<%--        {{#     }                                   }}--%>
        <input type="checkbox" name="status" value="{{= d.id }}" title="上架|下架"
               lay-skin="switch" lay-filter="statusFilter" {{= d.status == 1 ? "checked" : "" }}>
    </script>
    <script>
        layui.use(['form','table', 'upload'], function(){
            var table = layui.table;
            var form = layui.form;
            var upload = layui.upload;
            // 创建渲染实例
            table.render({
                elem: '#test',
                id: 'tableId',
                url:'/blog?method=selectByPage',
                toolbar: '#toolbarDemo',
                cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field:'id', width:60, title: 'ID', sort: true},
                    {field:'title', title: '博客名称', width: 150},
                    {field:'image', title: '博客封面', templet: '#imageTemplet', width: 200},
                    {field:'content', title: '博客内容', escape: false},
                    {field:'clickCount', title: '点击数量', width:120, sort: true},
                    {field:'status', title: '状态', templet: '#statusTemplet',width: 100},
                    // {field:'sign', title: '签名', minWidth: 150},
                    // {field:'experience', width:80, title: '积分', sort: true}
                    {fixed: 'right', title:'操作', width: 150, minWidth: 125, toolbar: '#barDemo'}
                ]],
                page: true
            });
            form.on('switch(statusFilter)', function(obj){
                //update blog set status=? where id=1;
                var id = this.value;
                var checked = obj.elem.checked;
                console.log(checked);
                var status = checked ? 1 : 0;
                $.post(
                    '/blog?method=updateStatus',
                    {'id': id, 'status': status},
                    function (result) {
                        if (result.code == 0) {
                            mylayer.okMsg(result.msg);
                        }
                    },
                    'json'
                );
            });
            form.on('submit(submitSearch)', function(data){
                var field = data.field; // 获得表单字段
                // 执行搜索重载
                table.reload('tableId', {
                    page: {
                        curr: 1 // 重新从第 1 页开始
                    },
                    where: field // 搜索的字段
                });
                return false; // 阻止默认 form 跳转
            });
            table.on('toolbar(test)', function(obj){
                var id = obj.config.id;
                var checkStatus = table.checkStatus(id);
                var othis = lay(this);
                switch(obj.event){
                    case 'add':
                        var index = layer.open({
                            title: '添加博客',
                            type: 1,
                            area: ['70%', '90%'],// 宽高
                            content: $('#addForm'),
                            success: function () {
                                $("#addForm form")[0].reset();
                                //form.render();
                                //先销毁再初始化
                                KindEditor.remove('#contentId');
                                var kindEditorParams = {
                                    afterBlur: function () {
                                        this.sync();
                                    }
                                };
                                var editor = KindEditor.create("#contentId", kindEditorParams);
                                editor.html("");
                                var uploadInst = upload.render({
                                    elem: '#uploadId',
                                    url: '/upload', // 实际使用时改成您自己的上传接口即可。
                                    before: function(obj){
                                        // 预读本地文件示例，不支持ie8
                                        obj.preview(function(index, file, result){
                                            $('#demo1').attr('src', result); // 图片链接（base64）
                                        });
                                    },
                                    done: function(result){
                                        // 若上传失败
                                        if(result.code == 0){
                                            $('#imageId').val(result.data);
                                            $('#demoText').html(''); //置空上传失败的状态
                                        } else {
                                            mylayer.errorMsg(result.msg);
                                        }
                                    },
                                    error: function(){
                                        // 演示失败状态，并实现重传
                                        var demoText = $('#demoText');
                                        demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                                        demoText.find('.demo-reload').on('click', function(){
                                            uploadInst.upload();
                                        });
                                    },
                                    // 进度条

                                    progress: function(n, elem, e){
                                        element.progress('filter-demo', n + '%'); // 可配合 layui 进度条元素使用
                                        if(n == 100){
                                            layer.msg('上传完毕', {icon: 1});
                                        }
                                    }
                                });
                            }
                        });
                        form.on('submit(submitAdd)',function (data) {
                            var field = data.field;
                            console.log(data);
                            $.post(
                                '/blog?method=add',
                                field,
                                function (result) {
                                    console.log(result);
                                    if (result.code == 0) {
                                        mylayer.okMsg(result.msg);
                                        layer.close(index);
                                        table.reload('tableId');
                                    } else {
                                        mylayer.errorMsg(result.msg);
                                    }
                                },
                                'json'
                            );
                            return false;
                        });
                        break;
                    case 'deleteAll':
                        var data = checkStatus.data;
                        //[{"id":2,"name":"Java1812","address":"ddf","time":"232"},{"id":3,"name":"Java1903","address":"34","time":"323"}]
                        var ids = new Array();
                        $(data).each(function () {
                            ids.push(this.id);
                        })
                        //[2,4]
                        layer.confirm(
                            '您确认要删除么?',
                            {icon : 3},
                            function(index){
                                $.post(
                                    '/blog?method=deleteAll',
                                    {'ids': ids},
                                    function (result) {
                                        console.log(result);
                                        if (result.code == 0) {
                                            mylayer.okMsg(result.msg);
                                            //删除成功之后刷新表格，展示最新数据
                                            table.reload('tableId');
                                        } else {
                                            mylayer.errorMsg(result.msg);
                                        }
                                    },
                                    'json'
                                );
                            }
                        );
                        break;
                    case 'LAYTABLE_TIPS':
                        layer.alert('自定义工具栏图标按钮');
                        break;
                };
            });
            table.on('tool(test)', function(obj){ // 双击 toolDouble
                var data = obj.data; // 获得当前行数据
                console.log(obj);
                if(obj.event === 'delete'){
                    layer.confirm(
                        '您确定要删除吗？',
                        {icon : 3},
                        function (index) {
                            $.post(
                                '/blog?method=deleteById',
                                {'id':data.id},
                                function (result) {
                                    console.log(result);
                                    if (result.code == 0) {
                                        mylayer.okMsg(result.msg);
                                        table.reload('tableId');
                                    } else {
                                        mylayer.errorMsg(result.msg);
                                    }
                                },
                                'json'
                            );
                        }
                    );
                } else if(obj.event === 'edit'){
                    var index = layer.open({
                        title: '编辑博客',
                        type: 1,
                        area: ['420px', '300px'], // 宽高
                        content: $('#updateForm').html(),
                        success: function () {
                            $.post(
                                '/blog?method=selectById',
                                {'id': data.id},
                                function (result) {
                                    if (result.code == 0) {
                                        form.val('updateFormFilter', result.data);
                                    }
                                },
                                'json'
                            );
                        }
                    });
                    form.on('submit(submitUpdate)',function (data) {
                        var field = data.field;
                        console.log(data);
                        $.post(
                            '/blog?method=update',
                            field,
                            function (result) {
                                console.log(result);
                                if (result.code == 0) {
                                    mylayer.okMsg(result.msg);
                                    layer.close(index);
                                    table.reload('tableId');
                                } else {
                                    mylayer.errorMsg(result.msg);
                                }
                            },
                            'json'
                        );
                        return false;
                    });
                } else if (obj.event === 'details'){
                    layer.open({
                        title: data.title,
                        type: 1,
                        area: ['800px', '600px'],// 宽高
                        content: data.content,
                    })
                }
            });
        });
    </script>
</body>
</html>
